package com.enzoism.springboot.fastjson.service;

import com.enzoism.springboot.fastjson.dao.ScoresDao;
import com.enzoism.springboot.fastjson.model.Scores;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 业务操作Service
 */
@Service
public class ScoresDBService {
    @Resource
    public ScoresDao scoresDao;

    /**
     * 列表
     */
    public List<Scores> selectDataList() {
        return scoresDao.selectDataList();
    }

    /**
     * 详情
     */
    public Scores selectByPrimaryKey(Integer id) {
        return scoresDao.selectByPrimaryKey(id);
    }

    /**
     * 更新
     */
    @Transactional
    public void updateByPrimaryKey(Scores scores) {
        scoresDao.updateByPrimaryKey(scores);
    }

    /**
     * 删除
     */
    @Transactional
    public void deleteByPrimaryKey(Scores scores) {
        scoresDao.deleteByPrimaryKey(scores.getId());
    }

    /**
     * 插入-返回自动生成的主键ID
     */
    @Transactional
    public Integer insertAndReturnAutoId(Scores scores) {
        scoresDao.insert(scores);
        return scores.getId();
    }

    /**
     * 插入-返回自动生成的主键ID-模拟业务逻辑失败
     */
    @Transactional
    public Integer insertAndReturnAutoIdRuntimeException(Scores scores) {
        scoresDao.insert(scores);
        throw new RuntimeException("模拟业务逻辑失败-RuntimeException");
    }

    /**
     * 插入-返回自动生成的主键ID-真实数据库插入失败
     */
    @Transactional
    public Integer insertAndReturnAutoIdSQlException(Scores scores) {
        scoresDao.insertSqlException(scores);
        return scores.getId();
    }
}
